package com.zjhcsoft.shop.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.stereotype.Controller;


import com.opensymphony.xwork2.ActionSupport;
import com.zjhcsoft.common.object.UploadObj;
import com.zjhcsoft.common.service.upload.AbstractUploadFile;
import com.zjhcsoft.common.service.upload.FileUploadService;
import com.zjhcsoft.common.service.upload.impl.FileUploadServiceImpl;

@Controller
public class UploadAction extends ActionSupport {
	private static final long serialVersionUID = 1359090410023337654L;
	private String flag; //上传类型标识，small表示生成小图，desc是描述性图片，直接保存
	private File file;
	private String fileFileName; //文件名
	private String fileContentType; //文件类型
	private UploadObj uploadObj; //图片上传成功返回对象
//	@Resource
//	FileUploadService fileUploadService;
	public String uploadFile(){
		try {
			
			FileInputStream is = new FileInputStream(file);  
			FileUploadService fileUploadService = new FileUploadServiceImpl();
			//uploadObj = fileUploadService.uploadFile(is,fileFileName,"product");
			if("small".equals(flag)){
				uploadObj = fileUploadService.uploadImgFile(is,fileFileName,50,AbstractUploadFile.SMALL_IMG, "product");
			}else{
				uploadObj = fileUploadService.uploadFile(is,fileFileName,"product");
			}
			
			System.out.println(uploadObj.getRelativePath());
			System.out.println(uploadObj.getRelativeSmallImgPath());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "success";
	}
	@JSON(serialize=false)
	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}
	@JSON(serialize=false)
	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	@JSON(serialize=false)
	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public UploadObj getUploadObj() {
		return uploadObj;
	}

	public void setUploadObj(UploadObj uploadObj) {
		this.uploadObj = uploadObj;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}

}
